# -*- encoding: utf-8 -*-
"""
@File  : genson.py
@Description : jsonschema生成器
"""
from json import dumps

from genson import SchemaBuilder
from lib.request import ResponseResult


def genson(data=None):
    """
    genson:
    https://github.com/wolverdude/GenSON
    """
    if (data is None) and ResponseResult.response is not None:
        data = ResponseResult.response

    builder = SchemaBuilder(schema_uri='http://json-schema.org/draft-07/schema#')
    builder.add_object(data)
    to_schema = builder.to_schema()

    json_schema = dumps(
        to_schema,
        ensure_ascii=False,
        sort_keys=True,
        indent=2,
    )

    return json_schema
